const express = require('express');
const mongoose = require('mongoose');
const Article = mongoose.model('Article');
const wechat = require('wechat');
const jssdk = require('../libs/jssdk');
const baseUrl = 'http://thnkwjitzi.localtunnel.me';

const router = express.Router();
module.exports = (app) => {
    app.use('/wechat', router);
};

router.get('/hello', (req, res, next) => {
  jssdk.getSignPackage(`${baseUrl}${req.url}`,function(err,signPackage){
    if(err){
      return next(err);
    }
    res.render('index',{
      title:'hello wechat',
      pretty:true
    })
  })
});
const config = {
    token: 'imoocsevendayslisteningwechatnode',
    openid: 'wx43d174d3c7c93dab'
}

const handleWechatRequest = wechat(config, function (req, res, next) {
  const message = req.wechat;
  console.info(message);
  res.reply('hello');
})
router.get('/conversation', handleWechatRequest);
router.post('/conversation', handleWechatRequest);
